<template>
  <div class="login" id="login">
    <div>
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
        label-width="100px"
      >
        <template>
          <div>
            <h1>小U商城后台管理</h1>
          </div>
        </template>
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            clearable
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            clearable
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login('loginForm')">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import particlesJson from "../assets/particles.json";
//引入封装好的登录接口
import { mapActions } from "vuex";
//引入封装好的登陆接口
import { userLogin } from "@/util/axios";
export default {
  data() {
    return {
      loginForm: {
        username: "", //用户名
        password: "", //密码
      },
      //封装一个rules规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  //组件内部守卫，离开前触发的守卫
  beforeRouteLeave(to, from, next) {
    if (sessionStorage.getItem("userInfo") == null) {
      location.reload(), next("/login");
    } else {
      next();
    }
  },
  mounted() {
    particlesJS("login", particlesJson);
    document.body.style.overflow = "hidden";
  },
  methods: {
    ...mapActions(["changeUserInfoAction"]),
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          userLogin(this.loginForm).then((res) => {
            if (res.data.code == 200) {
              //线存储然后再跳转
              this.$message.success(res.data.msg);
              //登录成功之后，把存储信息存到vuex中
              this.changeUserInfoAction(res.data.list);
              this.$router.push("/index");
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="stylus" scoped>
#login
  width 100vw;
  height 100vh;
  background-image url('../assets/img/1.jpg');
  background-size cover;
  background-position 50% 0%;
  background-repeat no-repeat;
.login
  background-image url('../assets/img/1.jpg');
  background-size cover;
  background-position 50% 0%;
  background-repeat no-repeat;
  .el-form
    position absolute;
    top 50%;
    left 50%;
    margin -150px 0 0 -180px;
    width 400px;
    height 250px;
    background-color #fff;
    h1
      text-align center;
      margin-bottom 25px;
    .el-input
      width 90%;
</style>